/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package daos;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import models.*;
import util.WebConst;
/**
 *
 * @author trietto
 */
public class RatingDAO {
private PreparedStatement pStmtSelectCount = null;
    private PreparedStatement pStmtSelectAll = null;
    private PreparedStatement pStmtSelect = null;
    private PreparedStatement pStmtInsert = null;
    private PreparedStatement pStmtDelete = null;
    private PreparedStatement pStmtUpdate = null;
    private static RatingDAO ratingDAO = null;

    public static RatingDAO singleton() {
        if (ratingDAO == null) {
            ratingDAO = new RatingDAO();
        }
        return ratingDAO;
    }

    public RatingDAO() {
        try {
            pStmtSelectCount = DBConnection.getConnection().prepareStatement("SELECT Count(ItemId) FROM Rating WHERE ItemId=?");
            pStmtSelectAll = DBConnection.getConnection().prepareStatement("SELECT * FROM Ratings WHERE ItemId = ? LIMIT ?,?");
            pStmtSelect = DBConnection.getConnection().prepareStatement("SELECT * FROM Ratings WHERE ItemId = ? and Customer = ?");
            pStmtInsert = DBConnection.getConnection().prepareStatement("INSERT INTO Ratings(ItemId, Customer, Comment, Rating) VALUES(?,?,?,?)");
            pStmtDelete = DBConnection.getConnection().prepareStatement("DELETE FROM Ratings WHERE ItemId = ? AND Customer = ?");
            pStmtUpdate = DBConnection.getConnection().prepareStatement("UPDATE Ratings SET Comment=?, Rating=? WHERE ItemId = ? AND Customer = ?");
        } catch (SQLException ex) {
            Logger.getLogger(RatingDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

   public int recordCount(int itemId){
        int totalRecord = 0;
        try {
            pStmtSelectCount.setInt(1, itemId);
            ResultSet rs = pStmtSelectCount.executeQuery();
            while (rs.next()) {
                totalRecord = rs.getInt(1);
            }
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(CardTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return totalRecord;
    }

   public ArrayList<Rating> selectAll(int itemId, int page) {
        ArrayList<Rating> lstRatings = new ArrayList<Rating>();
        int offset = (page-1) * WebConst.ROW_PER_PAGE;
        try {
            pStmtSelectAll.setInt(1, itemId);
            pStmtSelectAll.setInt(2, offset);
            pStmtSelectAll.setInt(3, WebConst.ROW_PER_PAGE);
            ResultSet rs = pStmtSelectAll.executeQuery();
            while (rs.next()) {
                lstRatings.add(new Rating(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4)));
            }
        } catch (SQLException ex) {
            Logger.getLogger(RatingDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return lstRatings;
    }

    public Rating select(int itemId, String customer) {
        Rating rating = null;
        try {
            pStmtSelect.setInt(1, itemId);
            pStmtSelect.setString(2, customer);
            ResultSet rs = pStmtSelect.executeQuery();
            while (rs.next()) {
                rating = new Rating(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4));
            }
        } catch (SQLException ex) {
            Logger.getLogger(RatingDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rating;
    }

    public String insert(Rating rating) {
        String result = "";
        boolean bRet = false;
        try {
            pStmtInsert.setInt(1, rating.getItemId());
            pStmtInsert.setString(2, rating.getCustomer());
            pStmtInsert.setString(3, rating.getComment());
            pStmtInsert.setInt(4, rating.getRating());
            pStmtInsert.execute();
            bRet = true;
        } catch (SQLException ex) {
            Logger.getLogger(RatingDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = bRet ? "Successed" : "Failed";
        return result;
    }

    public String delete(int itemId, String customer) {
        String result = "";
        int numOfRows = 0;
        try {
            pStmtDelete.setInt(1, itemId);
            pStmtDelete.setString(2, customer);
            numOfRows = pStmtDelete.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(RatingDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = numOfRows>0 ? "Successed" : "Failed";

        return result;
    }

    public String update(Rating rating) {
               String result = "";
        int numOfRows = 0;
        try {
            pStmtUpdate.setString(1, rating.getComment());
            pStmtUpdate.setInt(2, rating.getRating());
            numOfRows = pStmtUpdate.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(RatingDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = numOfRows>0 ? "Successed" : "Failed";

        return result;
    }
}
